
# V Final Exhibit - Figure 1: Concentration of spending V --------------------------
concentration_func <- function(data, sample_txt, group_txt) {
  data[,num_days_lived:=ifelse(DMG_died_within_365d == "0",
                               366,
                               as.numeric(difftime(DMG_date_of_death_XX,
                                                   S_index_date_XX, 
                                                   units = "days"))+1)][,
                                                                        .(.N,
                                                                          sum_day_lived   = sum(num_days_lived),
                                                                          sum_tot_cost    = sum(UTL_f365d_total_cost)), 
                                                                        by = .(DMG_died_within_365d)][
                                                                          ,
                                                                          .(sample = sample_txt,
                                                                            group = group_txt,
                                                                            DMG_died_within_365d,
                                                                            N=N/1,
                                                                            sum_n = sum(N)/1,
                                                                            N_pct = N/sum(N),
                                                                            days_pct = sum_day_lived/sum(sum_day_lived),
                                                                            cost_pct = sum_tot_cost/sum(sum_tot_cost))
                                                                          ][DMG_died_within_365d==1]
}

fig_concentration <- function( data_all = dt_all, data_cnr = dt_cnr ) {
  concentration_dt <- rbind(
    concentration_func(data_all, "all", "all"),
    concentration_func(data_all[DMG_age < 65], "all", "25to64"),
    concentration_func(data_all[DMG_age >= 65], "all", "65p"),
    concentration_func(data_cnr, "cancer", "all")
  )[, DMG_died_within_365d := NULL]
  
  concentration_dt_long <- data.table::melt.data.table(
    concentration_dt,
    id.vars = c("sample", "group")
  )[, sample_group := factor(paste0(sample, "_", group),
                             levels = c("all_all", "all_65p",
                                        "all_25to64" ,"cancer_all"))] 
  concentration_dt_long[, primary_group := (group == "all")]
  
  
  # Second version (B) - without general pop sample 25-64 # MAIN 
  pdf(file = "conc_of_cost_version_B.pdf")
  print (ggplot(data=concentration_dt_long[sample_group != "all_25to64" & sample_group != "all_65p" &
                                             !variable %in% c("N","sum_n")  ],
                aes(x = sample_group, y = value, group = variable, 
                    fill = variable, color=variable)) + 
           geom_bar(stat = "identity", 
                    position = position_dodge(width = c(0.7, 0.7)),
                    width=0.7) +
           scale_x_discrete(labels = c("General Population\nSample",
                                       "Cancer Sample")) + 
           scale_y_continuous(labels = scales::percent) +
           geom_text(aes(label=paste0(format(round(value*100,1),nsmall = 1),"%")), 
                     position= position_dodge(width = c(0.7, 0.7)), 
                     color = "black",
                     vjust=-0.25, 
                     size = 6) +
           scale_color_manual(values = c(NA, NA, "black"), guide = FALSE) +
           scale_fill_manual(name = "",
                             values  = c("grey40","black","grey90"),
                             labels = c("Decedent Share of Population",
                                        "Decedent Share of Days Lived",
                                        "Decedent Share of Spending"),
                             guide = guide_legend(nrow=3)) +
           labs(x = "", y = "") +
           theme(panel.grid.major.x = element_blank(), 
                 panel.grid.minor.x = element_blank() )
  )
  dev.off()
  
  write_csv(concentration_dt_long,"conc_of_cost_version_B.csv")
} 

